// pages/add_qx1/add_qx1.js
var common = require("../../utils/public.js")
var api = require("../../utils/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    array: [],
    index: 0,
    imgUrl:'',
    createid:'',
    shopId:'',
    authUserId:'',
    objects:''
  },
 
  // vant复选框
  onChange(event) {
    console.log(event.detail)
    this.setData({
      result: event.detail,
    });
  },
  // 跳转下一页
  add: common.throttleFunc(function (e) {
    let that=this
    if (that.data.result.length==0){
       wx.showToast({
         title: '请选择添加授权',
         icon:'none'
       })
    }else{
      if (that.data.objects == null) {
        api.addShops({
          authFunction: that.data.result.join(','),
          authUserId: that.data.authUserId,
          shopId: that.data.array[that.data.index].id,
        }).then(res => {
         if(res.result){
            wx.showToast({
              title: '添加成功',
              icon:'none',
              success:function(){
                wx.navigateBack();
              }
            })
          }
        })
      } else {
        api.modifyShop({
          authFunction: that.data.result.join(','),
          id:that.data.objects.id
        }).then(res => {
          if (res.result) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              success: function () {
                wx.navigateBack();
              }
            })
          }
        })
      }
      
    }
   
   
  }),
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    let that=this
    that.setData({
      imgUrl: options.imgUrl,
      authUserId:options.userId
    })
    api.shopList({

    }).then(res => {
      if (res.result) {
        that.setData({
          array:res.object,
          createid: res.object[0].userInfoEntity.id
        })
        

        that.jurisdiction()
      }
    })
    
  },


 // 选择picker
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    this.jurisdiction()
  },
  
  jurisdiction:function(){
    let that=this
    api.jurisdiction({
      authUserId: that.data.authUserId,
      shopId: that.data.array[that.data.index].id,
      createid: that.data.createid
    }).then(res => {
      if (res.result) {
        if (res.object!=null){
          that.setData({
            result: res.object.authFunction.split(',')
          })
          
        }
        that.setData({
          objects: res.object
         
        })
     
        
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})